package com.sirui.cloud.identity.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sirui.cloud.identity.dao.XxlJobInfoMapper;
import com.sirui.cloud.identity.domain.MsgDto;
import com.sirui.cloud.identity.domain.dto.LoginDto;
import com.sirui.cloud.identity.domain.entity.XxlJobInfoEntity;
import com.sirui.cloud.identity.svc.LoginSvc;
import com.sirui.x.global.LogArround;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 1. token 2. jwt
 */
@RestController
@Api(tags = "用户认证")
@RequestMapping("security")
public class LoginController {

	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

	@Autowired
	private XxlJobInfoMapper jobinfoMapper;
	@Autowired
	private LoginSvc loginSvc;

	@PostMapping("login1")
	@LogArround
	public MsgDto<XxlJobInfoEntity> loginForm1(@Valid LoginDto dto) {
		XxlJobInfoEntity jobInfo = jobinfoMapper.selectByPrimaryKey(4);
		return MsgDto.getSucc(jobInfo);
	}

	@PostMapping("login2")
	@LogArround(before = true, after = true)
	public MsgDto<?> loginForm2(String uname, String pwd) {
		try {
			loginSvc.login(uname, pwd);
			logger.info("String valid pass");
		} catch (Exception e) {
			logger.error("String valid unpass", e);
		}

		try {
			loginSvc.login(new LoginDto());
			logger.info("Logindto valid pass");
		} catch (Exception e) {
			logger.error("Logindto valid unpass", e);
		}
		return MsgDto.getSucc();
	}

	@PostMapping("login3")
	@ApiOperation(value = "plain登录")
	public MsgDto<?> loginPlain(@RequestBody LoginDto dto) {

		return MsgDto.getSucc();
	}
	
	@PostMapping("login4")
	@ApiOperation(value = "form登录")
	public MsgDto<?> loginForm(LoginDto dto) {

		return MsgDto.getSucc();
	}

}
